<!DOCTYPE html>
<html lang="zh_CN" xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"  xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
  <meta charset="utf-8">
  <title>语言管理列表  Permission List</title>
  <meta name="renderer" content="webkit">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, language-scalable=0">
  <link rel="stylesheet" th:href="@{/statics/layui/css/layui.css}" media="all">
  <link rel="stylesheet" th:href="@{/statics/style/admin.css}" media="all">
</head>
<body>

<div class="layui-fluid">
  <div class="layui-row layui-col-space15">
    <div class="layui-col-md12">
      <div class="layui-card">
        <div class="layui-card-header">语言管理列表</div>
        <div class="layui-card-body">
          <table class="layui-hide" id="language-list-table" lay-filter="language-list-filter"></table>

          <script type="text/html" id="language-list-table-operate-bar">
            <a shiro:hasPermission="sys:lang:edit" class="layui-btn layui-btn-xs" lay-event="edit"><i class="layui-icon layui-icon-edit"></i>编辑</a>
            <a shiro:hasPermission="sys:lang:remove" class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del"><i class="layui-icon layui-icon-delete"></i>删除</a>
          </script>

          <script type="text/html" id="language-list-table-toolbar">
            <div class="layui-btn-container">
                <button shiro:hasPermission="sys:lang:add" class="layui-btn layui-btn-sm" lay-event="add">添加语言</button>
            </div>
          </script>

        </div>
      </div>
    </div>
  </div>
</div>


<script th:src="@{/statics/layui/layui.js}"></script>
<script th:inline="none">
  layui.config({
    base: '/statics/' //静态资源所在路径
  }).extend({
    index: 'lib/index' //主入口模块
  }).use(['index', 'table'], function(){
    var $ = layui.$
    ,admin = layui.admin
    ,table = layui.table;

      var tableIns = table.render({
        elem: '#language-list-table'
        ,url: '/api/language/list'
        ,id:'id'
        ,toolbar: '#language-list-table-toolbar'
        ,cols: [[
          {field:'code', width:100, title: '代码', sort: true}
          ,{field:'name', width:180, title: '名称'}
          ,{field:'memo', title: '描述'}
          ,{field:'isDefault', width:80, title: '默认', sort: true ,templet:function(d){
              if(d.isDefault == 1){
                  return "是";
              }else{
                  return "否"
              }
            }
          }
          ,{field:'createTime', width:180, title: '加入时间', sort: true}
          ,{width:200, align:'center', toolbar: '#language-list-table-operate-bar'}
          ]]
    });

      //头工具栏事件
      table.on('toolbar(language-list-filter)', function(obj){
          var checkStatus = table.checkStatus('id');
          switch(obj.event){
              case 'add':
                  layer.open({
                      type: 2
                      ,title: '添加语言'
                      ,content: '/web/language/add'
                      ,maxmin: true
                      ,area: ['500px', '450px']
                      ,btn: ['确定', '取消']
                      ,yes: function(index, layero){
                          var iframeWindow = window['layui-layer-iframe'+ index]
                              ,submitID = 'LAY-lang-add-submit'
                              ,submit = layero.find('iframe').contents().find('#'+ submitID);

                          //监听提交
                          iframeWindow.layui.form.on('submit('+ submitID +')', function(data){
                              var field = data.field; //获取提交的字段

                              //提交 Ajax 成功后，静态更新表格中的数据
                              $.ajax({
                                  url: '/api/language/add',
                                  method: 'post',
                                  data: data.field,
                                  dataType:'JSON',
                                  success:function(res){
                                      if(res.code=='0'){
                                          tableIns.reload(); //数据刷新
                                          layer.close(index); //关闭弹层
                                      }else {
                                          alert(res.msg);
                                      }
                                  },
                                  error:function (err) {
                                  }
                              });

                          });

                          submit.trigger('click');
                      }
                  });
                  break;
          };
      });

      //监听行工具事件
      table.on('tool(language-list-filter)', function(obj){
          var data = obj.data;
          switch(obj.event) {
              case 'del':
                  layer.confirm('真的删除么', function (index) {
                      $.ajax({
                          url: '/api/language/removeFlag?id='+data.id,
                          method: 'put',
                          contentType: "application/json;charset=UTF-8",
                          dataType:'JSON',
                          success:function(res){
                              if(res.code=='0'){
                                  layer.msg('记录已删除!',{icon:1,time:1000},function () {
                                      obj.del();
                                      layer.close(index); //关闭弹层
                                  });
                              }else {
                                  layer.msg(res.msg);
                              }
                          },
                          error:function (err) {
                          }
                      });
                  });
                  break;
              case 'edit':
                  layer.open({
                      type: 2
                      ,title: '编辑语言'
                      ,content: '/web/language/edit?id='+ data.id
                      ,maxmin: true
                      ,area: ['500px', '450px']
                      ,btn: ['确定', '取消']
                      ,yes: function(index, layero){
                          var iframeWindow = window['layui-layer-iframe'+ index]
                              ,submitID = 'LAY-lang-add-submit'
                              ,submit = layero.find('iframe').contents().find('#'+ submitID);

                          //监听提交
                          iframeWindow.layui.form.on('submit('+ submitID +')', function(data){
                              var field = data.field; //获取提交的字段

                              //提交 Ajax 成功后，静态更新表格中的数据
                              $.ajax({
                                  url: '/api/language/edit',
                                  method: 'post',
                                  data: data.field,
                                  dataType:'JSON',
                                  success:function(res){
                                      if(res.code=='0'){
                                          tableIns.reload(); //数据刷新
                                          layer.close(index); //关闭弹层
                                      }else {
                                          alert(res.msg);
                                      }
                                  },
                                  error:function (err) {
                                  }
                              });

                          });

                          submit.trigger('click');
                      }
                  });
                  break;
          }
      });

  });
</script>
</body>
</html>